package com.byinsight.web.actions;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

import eg.backend.manager.UserManager;

@SuppressWarnings("serial")
abstract public class BaseAction extends ActionSupport implements Preparable, ServletRequestAware, ServletResponseAware,
SessionAware, ServletContextAware {
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Map<String, Object> session;
	protected ServletContext application;
	protected String actionMethod;
	
	//for xcelsius. remove when all turn to flex
	private String source;
	
	protected UserManager userManager;
	
	abstract protected String process();
	private static final Logger logger = Logger.getLogger(BaseAction.class.getName());
	
	@Override
	public String execute() throws Exception {
		String ret = "";
		try {
			ret = process();
			ret = writeXml("100", "OK", ret);
		} catch (WebException we) {
			ret = writeXml("" + we.getCode(), we.getMsg(), "");
		} catch (Throwable e) {
			ret = writeXml("-1", "Unhandled exception: " + e, "");
			StringWriter sw = new StringWriter();   
            e.printStackTrace(new PrintWriter(sw));   
            logger.warning("exception: " + sw.toString());   
		}
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/xml");
		response.getWriter().println(ret);
		return null;
	}

	private String writeXml(String code, String message, String body) {
		StringBuffer resp = new StringBuffer();
		resp.append("<envelop><header>");
		resp.append("<code>").append(code).append("</code>");
		resp.append("<message>").append(message).append("</message>");
		resp.append("</header>");
		resp.append("<body>").append(body).append("</body></envelop>");
		return resp.toString();
	}
	
	/* common */
	public void setServletContext(ServletContext context) {
		this.application = context;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		this.response.setCharacterEncoding("utf-8");
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		try {
			this.request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
		}
	}
	
	protected String getUTF8String(String input) {		
//		String ret = null;
//		try {
//			ret = new String(input.getBytes("ISO8859-1"), "utf-8");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
		return input;
	}

	@SuppressWarnings("unchecked")
	public void setSession(Map session) {
		this.session = session;
	}
	
	public void prepare() throws Exception {
//		super.p
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

}
